<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;

use think\Db;

class Secondhandletruck extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}

	/**
	*二手车详情页面         
	*@param  int id 		  车的id
	**/
	public function carDetail($id)
	{
		//查询二手车详情数组
		$carInfo = Db('used_truck')->where('id',$id)->select();
		$reading  = $carInfo[0]['reading'] + 1;
		if(Db('used_truck')->where('id', $id)->update(['reading' => $reading])){
			$carInfo1 = $this->dohtml($carInfo);
			//车型,品牌,车系,型号,获取信息放入数组中
			$carInfo1[0]['type'] = $this->dofind($carInfo1[0]['type']);
			$carInfo1[0]['brand'] = $this->dofind($carInfo1[0]['brand']);
			$carInfo1[0]['series'] = $this->dofind($carInfo1[0]['series']);
			$carInfo1[0]['version']= $this->dofind($carInfo1[0]['version']);
			//获得二手车的地址
			$arr = explode(',',$carInfo1[0]['site']);
			for($i = 0;$i < count($arr);$i++){
				$siteid = $arr[$i];
				$sitearr = Db('area')->where('id',$siteid)->select();
				$sitearr1[] = $sitearr[0]['name'];
			}
			$carInfo1[0]['site'] = implode(',',$sitearr1);
			return $carInfo1;
		}	
		
	}

	/**
	*公共方法:取出车型,品牌,车系,型号
	**/
	public function dofind($id)
	{
		$typeInfo = Db('used_classify')->where('id',$id)->select();
		return $typeInfo[0]['name'];

	}

	/**
	*二手车详情页面卖家信息
	**/
	public function seller()
	{
		$sellerInfo = Db('used_linkman')->where('id',1)->select();
		$seller = unserialize($sellerInfo[0]['value']);
		return $seller;
	}

	/**
	*公共方法:将数组中的图集,车辆所在地,标题,表显里程,全款价格,上牌时间,交强险过期时间,卖家附言进行处理
	**/
	public function dohtml($arr)
	{
		$i = 0;
		for(;$i<count($arr);$i++){
			$arr[$i]['photo'] = unserialize($arr[$i]['photo']);
			$arr[$i]['title'] = htmlspecialchars_decode($arr[$i]['title']);
			$arr[$i]['mileage'] = htmlspecialchars_decode($arr[$i]['mileage']);
			$arr[$i]['price'] = htmlspecialchars_decode($arr[$i]['price']);
			$arr[$i]['apply_time'] = htmlspecialchars_decode($arr[$i]['apply_time']);
			$arr[$i]['past_time'] = htmlspecialchars_decode($arr[$i]['past_time']);
			$arr[$i]['content'] = htmlspecialchars_decode($arr[$i]['content']);
			$arr1[] = $arr[$i];
		}
		return $arr1;		
	}

	/**
	*公共方法:将数组进行判断
	*@param  $arr		  要判断的数组
	**/
	public function doif($arr)
	{
		if($arr){
            $this->apiSuccess($arr);
        }else{
            $this->apiError('没有查到信息');
        }
	}


	/**
	*二手车详情页面同价位
	*@param  int id 		  车的id
	*@param  int disparity 	  车的差价
	**/
	public function priceLike($id,$disparity,$page,$pagelimit)
	{
		//查询二手车详情数组
		$carInfo = Db('used_truck')->where('id',$id)->select();
		$price = $carInfo[0]['price'];
		$minprice = $price - $disparity;
		$maxprice = $price + $disparity;
		//查询数据
		$carInfos = Db('used_truck')->where('price','>=',$minprice)->where('price','<=',$maxprice)->page($page,$pagelimit)->select();
		//返回总页数
		$carInfos = $this->dohtml($carInfos);
		$count = count($carInfos);
		$pagecount = ceil($count/$pagelimit);
		$list['group'] = $carInfos;
		$list['pagecount'] = $pagecount;
		return $list;

		// return $carInfos;
	}

	/**
	*二手车详情页面同品牌
	*@param  int id 		  车的id
	**/
	public function samebrand($id,$page,$pagelimit)
	{
		//查询相同品牌的二手车
		$carInfo = Db('used_truck')->where('id',$id)->select();
		$brand = $carInfo[0]['brand'];
		$carInfos = Db('used_truck')->where('brand',$brand)->page($page,$pagelimit)->select();
		$carInfos = $this->dohtml($carInfos);
		//返回总页数
		$count = count($carInfos);
		$pagecount = ceil($count/$pagelimit);
		$list['group'] = $carInfos;
		$list['pagecount'] = $pagecount;
		return $list;
	}

	/**
	*二手车在线咨询页面
	*@param  int id 		  车的id
	**/
	public function consult()
	{
		$id = $_GET['id'];
		//查询二手车详情数组
		$carInfo = Db('used_truck')->where('id',$id)->select();
		$carInfo1 = $this->dohtml($carInfo);
		$this->doif($carInfo1);
	}

	/**
	*二手车在线添加咨询
	*@param  int uid 		  咨询用户的id
	**/
	public function consultAdd()
	{
		//获取页面穿过来的数据
		$uid = $_POST['uid'];
		$create_time = date('Y-m-d H:i:s',strtotime('now'));
		$content = htmlspecialchars($_POST['content']);
		$phone = htmlspecialchars($_POST['phone']);	
		$name = htmlspecialchars($_POST['name']);

		//存入数据库
		$data = [
			'name'=>$name,
			'content'=>$content,
			'phone'=>$phone,
			'create_time'=>$create_time
		];
		$add = Db::name('used_consult')->insert($data);

		if($add){
			$this->apiSuccess('','添加成功');
		}else{
			$this->apiError('添加失败,请重试');
		}
	}

	/**
	*二手车卖车发布     (未测试)
	*@param  		id , 		'卖车用户的id'
	*@param  		title  		'标题',
	*@param  		type 		'车型',
	*@param  		brand  		'品牌',
	*@param  		series  	'车系',
	*@param  		version 	'型号',
	*@param  		site  		'车辆所在地',
	*@param  		mileage 	'表显里程',
	*@param  		price  		'价格',
	*@param  		methodtype '1全款,0分期',
	*@param  		apply_time  '上牌时间',
	*@param  		past_time  	'强险过期时间',
	*@param  		content  	'卖家附言',
	*@param  		image  		'图片集',
	**/
	public function sellCar()
	{
		//用户传入的数据
		$uid = $_POST['id'];
		$price = htmlspecialchars($_POST['price']);
		$mileage = htmlspecialchars($_POST['mileage']);
		$apply_time = htmlspecialchars($_POST['apply_time']);
		$past_time = htmlspecialchars($_POST['past_time']);
		$title = htmlspecialchars($_POST['title']);
		$content = htmlspecialchars($_POST['content']);
		$type = $_POST['type'];
		$brand = $_POST['brand'];
		$series = $_POST['series'];
		$version = $_POST['version'];
		$methodtype = $_POST['methodtype'];
		//拼接地址
		$site1 = $_POST['site'];
		$site2 = (floor($site1/1000))*1000;
		$site3 = array($site1,$site2);
		$site = implode(',', $site3);

		// 获取表单上传文件
		$files = request()->file($image);
		if($files){
			foreach($files as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
					// 成功上传后 将成功的文件名存入数组
					$arr[] = $info->getFilename();
				}else{
					// 上传失败获取错误信息
					echo $file->getError();
				}
			}
			//遍历拼接数组名字
			foreach($arr as $photoName){
				$data = date('Ymd',strtotime('now'));
				$path = '/uploads/'.$data.'/'.$photoName;
				$photo[] = $path;
			}
			//将数组序列化之后存入数据库
			$photo = serialize($photo);
		}else{
			$photo = '';
		}
		

		$create_time = date('Y-m-d H:i:s',strtotime('now'));
		$data = [
			'coding' => $this->svji(6),
			'user_id'=>$uid,
			'mileage'=>$mileage,
			'title'=>$title,
			'content'=>$content,
			'photo'=>$photo,
			'price'=>$price,
			'create_time'=>$create_time,
			'apply_time'=>$apply_time,
			'past_time'=>$past_time,
			'type'=>$type,
			'brand'=>$brand,
			'series'=>$series,
			'version'=>$version,
			'site'=>$site,
			'methodtype'=>$methodtype,
		];

		$add = Db::name('used_truck')->insert($data);
		if($add){
			$this->apiSuccess('','添加成功');
		}else{
			$this->apiError('添加失败,请重试');
		}
	}

	/**
	*随机生成字符串
	**/
	public function svji($pw_length =  '')
    { 
        $randpwd =''; 
        for ($i = 0; $i < $pw_length; $i++) 
        { 
            $randpwd .= chr(mt_rand(97, 122));
        } 
        return str_shuffle($randpwd.time());
    } 


    /**
	*二手车详情页面总接口
	**/
	public function detail()
	{
		//获取参数
		$id = $_GET['id'];
		$disparity = $_GET['disparity'];
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		//放进一个数组
		$List['carDetail'] = $this->carDetail($id);
		$List['priceLike'] = $this->priceLike($id,$disparity,$page,$pagelimit);
		$List['samebrand'] = $this->samebrand($id,$page,$pagelimit);
		$List['seller'] = $this->seller();
		$this->doif($List);
	}


}